// 日志信息落地
// 1.抽象落地基类
// 2.派生子类
// 3.工厂模式
#ifndef __M__SINK__
#define __M__SINK__
#include <iostream>
#include <memory>
#include <cassert>
#include <sstream>
#include <fstream>
#include "utils.hpp"

namespace wzh
{
    class LogSink
    {
    public:
        virtual ~LogSink() {}
        virtual void log(const char *data, size_t len) = 0;
        using ptr = std::shared_ptr<LogSink>;
    };
    // 标准输出
    class StdoutSink : public LogSink
    {
    public:
        void log(const char *data, size_t len)
        {
            std::cout.write(data, len);
        }
    };
    // 指定文件
    class FileSink : public LogSink
    {
    public:
        FileSink(const std::string &pathname)
            : _pathname(pathname)
        {
            util::File::createDirectory(util::File::path(pathname));
            _ofs.open(_pathname, std::ios::binary | std::ios::app);
            assert(_ofs.is_open());
        }
        void log(const char *data, size_t len)
        {
            _ofs.write(data, len);
            assert(_ofs.good());
        }

    private:
        std::string _pathname;
        std::ofstream _ofs;
    };
    // 滚动文件（文件大小）
    class RollSink_Size : public LogSink
    {
    public:
        RollSink_Size(const std::string &basename, size_t max_size)
            : _basename(basename), _max_fsize(max_size), _cur_fsize(0), _name_count(0)
        {
            std::string pathname = createNewFile();
            util::File::createDirectory(util::File::path(pathname));
            _ofs.open(pathname, std::ios::binary | std::ios::app);
            assert(_ofs.is_open());
        }
        void log(const char *data, size_t len)
        {
            if (_cur_fsize >= _max_fsize)
            {
                _ofs.close();
                std::string pathname = createNewFile();
                _ofs.open(pathname, std::ios::binary | std::ios::app);
                assert(_ofs.is_open());
                _cur_fsize = 0;
            }
            _ofs.write(data, len);
            assert(_ofs.good());
            _cur_fsize += len;
        }

    private:
        std::string createNewFile() // 进行大小判断，超出则创建新的文件
        {
            time_t t = util::Date::nowTime();
            struct tm lt;
            localtime_r(&t, &lt);
            std::stringstream filename;
            filename << _basename;
            filename << lt.tm_year + 1900;
            filename << lt.tm_mon + 1;
            filename << lt.tm_mday;
            filename << lt.tm_hour;
            filename << lt.tm_min;
            filename << lt.tm_sec;
            filename << "-";
            filename << _name_count++;
            filename << ".log";
            return filename.str();
        }

    private:
        size_t _name_count; 
        std::string _basename; // 基础文件名   base-20241222122423.log
        std::ofstream _ofs;
        size_t _max_fsize; // 最大文件大小
        size_t _cur_fsize; // 当前文件大小
    };

    
    class SinkFactory
    {
    public:
        template <typename SinkType, typename... Args>
        static LogSink::ptr create(Args &&...args)
        {
            return std::make_shared<SinkType>(std::forward<Args>(args)...);
        }
    };
}
#endif